package com.git.hui.yinshua.biz.goods.service;

import com.git.hui.yinshua.api.model.ListRsp;
import com.git.hui.yinshua.api.model.PageReq;
import com.git.hui.yinshua.biz.goods.dao.domain.CategoryEntity;
import com.git.hui.yinshua.biz.goods.dao.repository.CategoryRepository;
import com.git.hui.yinshua.biz.goods.service.convert.GoodsConvert;
import com.git.hui.yinshua.biz.rest.models.goods.req.CategoryQueryReq;
import com.git.hui.yinshua.biz.rest.models.goods.req.CategoryReq;
import com.git.hui.yinshua.biz.rest.models.goods.rsp.CategoryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author YiHui
 * @date 2024/6/12
 */
@Service
public class CategoryService {
    public static final Integer ENABLE_STATE = 1;
    public static final Integer DISABLE_STATE = 0;

    @Autowired
    private CategoryRepository categoryRepository;

    @Cacheable(cacheNames = "ys", key = "'category' + #cid", cacheManager = "caffeineCacheManager")
    public String getCategoryName(Integer cid) {
        return categoryRepository.getCategoryName(cid);
    }

    /**
     * 查询所有生效的类目列表
     *
     * @return
     */
    @Cacheable(cacheNames = "ys", key = "'category_list'", cacheManager = "caffeineCacheManager")
    public List<CategoryDto> listTotalEffectCategory() {
        PageReq req = new PageReq();
        List<CategoryEntity> list = categoryRepository.listAllOnlineCategory();
        return GoodsConvert.batchToCategory(list);
    }

    /**
     * 分类列表
     *
     * @param page
     * @return
     */
    public ListRsp<CategoryDto> categoryList(CategoryQueryReq page) {
        // 查询列表
        List<CategoryEntity> list = categoryRepository.list(page);
        ListRsp<CategoryDto> rsp = ListRsp.newRsp(GoodsConvert.batchToCategory(list), page.getPage(), page.getSize());
        if (page.getPage() == 1) {
            // 只有查询首页，且首页不是最后一页时，才需要补齐总数；其他的不用补齐总数，减少一次db查询
            if (rsp.getEnd()) {
                rsp.setTotal(list.size());
            } else {
                rsp.setTotal(categoryRepository.count());
            }
        }
        return rsp;
    }


    /**
     * 新增分类
     *
     * @param req
     * @return
     */
    @CacheEvict(cacheNames = "ys", allEntries = true, cacheManager = "caffeineCacheManager")
    public CategoryDto save(CategoryReq req) {
        CategoryEntity entity = categoryRepository.save(req.getCategory(), req.getIntro(), req.getEnable() ? ENABLE_STATE : DISABLE_STATE, req.getSort());
        return GoodsConvert.toCategory(entity);
    }

    /**
     * 启用/禁用 分类
     *
     * @param categoryId 分类id
     * @param enabled    true 启用 false 禁用
     * @return true 更行成功
     */
    @CacheEvict(cacheNames = "ys", allEntries = true, cacheManager = "caffeineCacheManager")
    public Boolean enable(Integer categoryId, Boolean enabled) {
        CategoryEntity entity = new CategoryEntity();
        entity.setId(categoryId);
        entity.setState(enabled ? ENABLE_STATE : DISABLE_STATE);
        return categoryRepository.update(entity);
    }

    @CacheEvict(cacheNames = "ys", allEntries = true, cacheManager = "caffeineCacheManager")
    public Boolean remove(Integer categoryId) {
        CategoryEntity entity = new CategoryEntity();
        entity.setId(categoryId);
        entity.setDeleted(1);
        return categoryRepository.update(entity);
    }

    /**
     * 更新分类信息
     *
     * @param req 分类信息
     * @return
     */
    @CacheEvict(cacheNames = "ys", allEntries = true, cacheManager = "caffeineCacheManager")
    public Boolean update(CategoryReq req) {
        CategoryEntity entity = new CategoryEntity();
        entity.setId(req.getCid());
        if (req.getCategory() != null) {
            entity.setCategory(req.getCategory());
        }
        if (req.getSort() != null) {
            entity.setSort(req.getSort());
        }
        if (req.getIntro() != null) {
            entity.setIntro(req.getIntro());
        }
        if (req.getEnable() != null) {
            entity.setState(req.getEnable() ? ENABLE_STATE : DISABLE_STATE);
        }
        return categoryRepository.update(entity);
    }
}
